<?php
class NewsController extends AppController {

	var $name = 'News';
	var $uses=array('Category','News');
	function index() {
		//$lang=$this->Session->read('lang');
		$lang=0;
		$this->News->recursive = 0;
		$conditions=array('News.lang_id'=>$lang);
		$this->paginate = array('conditions' =>$conditions,'order'=>'News.id DESC','limit' =>20);
        $tintucs = $this->paginate('News');
		$this->set('tintucs', $tintucs);
		$dk=array('Category.type'=>2,'Category.lang_id'=>$lang);
        $categorylist = $this->Category->generatetreelist($dk,null,null," _ ");
		$this->set(compact('categorylist'));

	}
	function add() {
		//$lang=$this->Session->read('lang');
		$lang=0;
		if (!empty($this->data))  {	
		    $this->News->create();
			
			$alias=strtolower(khongdau($this->data['News']['name']));
			$this->data['News']['alias']= Inflector::slug($alias,'-');
			$this->data['News']['lang_id']=$lang;
			//pr($this->data);die();
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('Thêm mới danh mục thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Thêm mơi danh mục thất bại. Vui long thử lại', true));
			}
		}
		$conditions=array('Category.type'=>2,'Category.lang_id'=>$lang);
        $categorylist = $this->Category->generatetreelist($conditions,null,null," _ ");
		$this->set(compact('categorylist'));
	}
    
	function edit($id=null) {
		//$lang=$this->Session->read('lang');
		$lang=0;
		if (!empty($this->data)) {
			$alias=strtolower(khongdau($this->data['News']['name']));
			$this->data['News']['alias']= Inflector::slug($alias,'-');
			$this->data['News']['lang_id']=$lang;
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('Bài viết sửa thành công', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Bài viết này không sửa được vui lòng thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->News->read(null, $id);
		}
		$conditions=array('Category.type'=>2,'Category.lang_id'=>$lang);
        $categorylist = $this->Category->generatetreelist($conditions,null,null," _ ");
		$this->set(compact('categorylist'));

	}
	function options() {
		//$lang=$this->Session->read('lang');
		$lang=0;
		if(!empty($_POST['keyword']) && isset($_POST['keyword'])){
			if($_POST['keyword']){
				$keyword = $_POST['keyword'];
				$this->Session->write('keyword',$keyword);
			}else{
			$keyword=$this->Session->read('keyword');
			}		
			
			$conditions=array('News.lang_id'=>$lang,'OR'=>array('News.name LIKE'=>'%'.$keyword.'%','News.introduction LIKE'=>'%'.$keyword.'%','News.content LIKE'=>'%'.$keyword.'%'));
			$this->paginate = array('conditions' =>$conditions,'order'=>'News.id DESC','limit' =>20);
			$tintucs = $this->paginate('News');			
			$this->set('tintucs', $tintucs);			
			$this->loadModel("Category");
			$dk1=array('Category.type'=>2,'Category.lang_id'=>$lang);
			$categorylist = $this->Category->generatetreelist($dk1,null,null," _ ");
			$this->set(compact('categorylist'));
			
		}
		elseif(!empty($this->data['category_id']) && isset($this->data['category_id'])){
			if($this->data['category_id']){
				$CAID = $this->data['category_id'];
				$this->Session->write('CAID',$CAID);
			}else{
			$CAID=$this->Session->read('CAID');
			}			
			$conditions=array('News.lang_id'=>$lang,'News.category_id'=>$CAID);
			$this->paginate = array('conditions' =>$conditions,'order'=>'News.id DESC','limit' =>20);
			$tintucs = $this->paginate('News');
			$this->set('tintucs', $tintucs);
				
			$this->loadModel("Category");
			$dk1=array('Category.type'=>2,'Category.lang_id'=>$lang);
			$categorylist = $this->Category->generatetreelist($dk1,null,null," _ ");
			$this->set(compact('categorylist'));
		}
		
		else{
			$this->redirect(array('action' => 'index'));
		}
	}

	function delete() {
		if($_POST['chkId']){
			$Idnews=$_POST['chkId'];
			foreach($Idnews as $id){
				$this->News->delete($id);			
			}
			$this->Session->setFlash(__('Bạn Delete thành công', true));
			$name=$_SERVER['HTTP_REFERER'];
			$this->redirect($name);
		}

	}

}
?>